home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / readobj.zip / READOBJ.C < prev    next >
Text File  |  1987-10-07  |  26KB  |  1,028 lines

  1. /****************************************************************
  2. * name         readobj -- read and format obj files             *
  3. *                         into a printable output               *
  4. *                                                               *
  5. * Original by Kip Davidson (73247,620)                          *
  6. * Upgraded to handle OS/2 object & libs by Dave Cortesi (72155,450) *
  7. *                                                               *
  8. * usage        readobj filename                                 *
  9. *                                                               *
  10. *              where "filename" is the name of an object code   *
  11. *              file, e.g. ROB.OBJ, or else the name of an       *
  12. *              object library, e.g. DOSCALLS.LIB.               *
  13. *                                                               *
  14. ****************************************************************/
  15.  
  16. /****************
  17. * Include files *
  18. *****************/
  19. #define LINT_ARGS
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #include <string.h>
  24.  
  25. /********************************
  26. * Microsoft Object Record Types *
  27. ********************************/
  28. #define BLKDEF  0x7A
  29. #define BLKEND  0x7C
  30. #define THEADR  0x80
  31. #define COMENT  0x88
  32. #define MODEND  0x8A
  33. #define EXTDEF  0x8C
  34. #define TYPDEF  0x8E
  35. #define PUBDEF  0x90
  36. #define LINNUM  0x94
  37. #define LNAMES  0x96
  38. #define SEGDEF  0x98
  39. #define GRPDEF  0x9A
  40. #define FIXUPP  0x9C
  41. #define LEDATA  0xA0
  42. #define LIDATA  0xA2
  43. #define COMDEF  0xB0    /* communal-names definition */
  44.  
  45. /********************************
  46. * Intel Object Record Types     *
  47. ********************************/
  48. #define RHEADR  0x6E
  49. #define REGINT  0x70
  50. #define REDATA  0x72
  51. #define RIDATA  0x74
  52. #define OVLDEF  0x76
  53. #define ENDREC  0x78
  54. #define DEBSYM  0x7E
  55. #define LHEADR  0x82
  56. #define PEDATA  0x84
  57. #define PIDATA  0x86
  58. #define LOCSYM  0x92
  59. #define LIBHED  0xA4
  60. #define LIBNAM  0xA6
  61. #define LIBLOC  0xA8
  62. #define LIBDIC  0xAA
  63.  
  64. /************************
  65. * Miscellaneous defines *
  66. ************************/
  67. #define NUM_ARGS  2
  68. #define MAX_LNAME_ENTRIES  100
  69. #define MAX_NAME_LEN        64
  70. #define MAX_SNAME_ENTRIES  100
  71. #define SNAME_SEG   0
  72. #define SNAME_CLASS 1
  73. #define SNAME_OVL   2
  74. #define GRPDEF_SI   0xff
  75. #define CV_ARRAY  0x6281   /* Codeview array indicator? */
  76. #define BLANK   ' '
  77. #define ZERO    0x00
  78. #define NOSWAP  00
  79. #define SWAP    01
  80. #define BIT7    0x80
  81. #define BIT6    0x40
  82. #define BIT5    0x20
  83. #define BIT4    0x10
  84. #define BIT3    0x08
  85. #define BIT2    0x04
  86. #define BIT1    0x02
  87. #define BIT0    0x01
  88.  
  89. /*********************
  90. * Error code defines *
  91. *********************/
  92. #define USAGE_ERR  1
  93. #define OPEN_ERR   2
  94.  
  95. /*********
  96. * Macros *
  97. **********/
  98. #define MALLOC(x)      ((x *) malloc(sizeof(x)))
  99. #define CALLOC(n,x)    ((x *) calloc(n, sizeof(x)))
  100.  
  101. /***********
  102. * Typedefs *
  103. ************/
  104. typedef unsigned char byte;
  105. typedef unsigned char * p_byte;     /* pointer to a byte */
  106. typedef unsigned short word;
  107. typedef unsigned short * p_word;    /* pointer to a word */
  108.  
  109. /****************
  110. * File pointers *
  111. ****************/
  112. FILE *fp1;              /* object code file pointer */
  113.  
  114. /*******************
  115. * Global variables *
  116. *******************/
  117. word stoppit;          /* flag for end of file */
  118. word i=0,j=0,k=0;      /* misc index counters */
  119. byte c=0,c1=0,c2=0;    /* misc unsigned char variables */
  120. word reclen=0;         /* length of record */
  121. byte num_bytes=0;      /* length of string passed */
  122. word chksum_count=0;   /* total bytes read for a record */
  123. byte chksum=0;         /* the checksum from the obj file */
  124. byte Lnames_index=1;   /* number of LNAMES entries */
  125. byte Lnames_total=0;   /* number of LNAMES entries */
  126.                        /* storage for LNAMES data */
  127. byte Lnames [MAX_LNAME_ENTRIES] [MAX_NAME_LEN+1] =
  128. {'*','N','U','L','L','*','\0'};
  129. byte Snames_index=1;   /* number of SNAMES entries */
  130. byte Snames_total=0;   /* number of SNAMES entries */
  131.                        /* storage for SNAMES data */
  132. word Snames [MAX_SNAME_ENTRIES] [SNAME_OVL+1] =
  133. { {0,0,0} };
  134. byte *align_msg[] =    /* SEGDEF align messages */
  135. {
  136.   "0 Absolute segment",
  137.   "1 Relocatable and byte-aligned segment",
  138.   "2 Relocatable and word-aligned segment",
  139.   "3 Relocatable and paragraph-aligned segment",
  140.   "4 Relocatable and page-aligned segment",
  141. };
  142. byte *combine_msg[] =  /* SEGDEF combine messages */
  143. {
  144.   "0 Private segment",
  145.   "1 ?",
  146.   "2 Public segment",
  147.   "3 ?",
  148.   "4 ?",
  149.   "5 Stack segment",
  150.   "6 Common segment",
  151. };
  152.  
  153. byte chr_buff[256];    /* string buffer */
  154.  
  155. /**************************************************************
  156. * This program uses the Object Record Types as described in   *
  157. * the following documents :                                   *
  158. *                                                             *
  159. * Intel :     8086 Relocatable Object Module Formats          *
  160. *             Version 4.0, Order # 121748-001                 *
  161. * Microsoft : MS-DOS Programmers Reference                    *
  162. *             Document # 8411-310-02                          *
  163. *             Part # 036-014-012                              *
  164. ***************************************************************/
  165. word main(argc,argv)
  166. word argc;
  167. byte *argv[];
  168. {
  169. void do_blkdef(void);
  170. void do_blkend(void);
  171. void do_comdef(void);
  172. void do_coment(void);
  173. void do_extdef(void);
  174. void do_fixupp(void);
  175. void do_grpdef(void);
  176. void do_ledata(void);
  177. void do_lidata(void);
  178. void do_linnum(void);
  179. void do_lnames(void);
  180. void do_modend(void);
  181. void do_pubdef(void);
  182. void do_locsym(void);
  183. void do_segdef(void);
  184. void do_theadr(void);
  185. void do_typdef(void);
  186. void do_unimplt(void);
  187. void exit(int);
  188. byte get_obj_byte(void);
  189.  
  190. if (argc != NUM_ARGS)
  191.    {
  192.     fputs("Usage: readobj filename \n", stderr);
  193.     exit(USAGE_ERR);
  194.    }
  195.  
  196. if ((fp1 = fopen(argv[1], "rb")) == NULL)
  197.    {
  198.     fprintf(stderr,"Can't open %s\n",argv[1]);
  199.     exit(OPEN_ERR);
  200.    }
  201.  
  202. printf("READOBJ by Kip Davidson (73247,620)\n");
  203. printf("\tOS/2 version Dave Cortesi (72155,450)\n\n");
  204.  
  205. c = get_obj_byte();
  206. while (stoppit == 0)
  207.    {
  208.     switch (c)
  209.      {
  210.       case BLKDEF :
  211.             do_blkdef();
  212.             break;
  213.       case BLKEND :
  214.             do_blkend();
  215.             break;
  216.       case THEADR :
  217.             do_theadr();
  218.             break;
  219.       case COMENT :
  220.             do_coment();
  221.             break;
  222.       case MODEND :
  223.             do_modend();
  224.             break;
  225.       case EXTDEF :
  226.             do_extdef();
  227.             break;
  228.       case TYPDEF :
  229.             do_typdef();
  230.             break;
  231.       case PUBDEF :
  232.             do_pubdef();
  233.             break;
  234.       case LOCSYM :
  235.             do_locsym();
  236.             break;
  237.       case LINNUM :
  238.             do_linnum();
  239.             break;
  240.       case LNAMES :
  241.             do_lnames();
  242.             break;
  243.       case SEGDEF :
  244.             do_segdef();
  245.             break;
  246.       case GRPDEF:
  247.             do_grpdef();
  248.             break;
  249.       case FIXUPP :
  250.             do_fixupp();
  251.             break;
  252.       case LEDATA :
  253.             do_ledata();
  254.             break;
  255.       case LIDATA :
  256.             do_lidata();
  257.             break;
  258.       case COMDEF :
  259.             do_comdef();
  260.             break;
  261.       case 0x00 : /* null bytes used in LIB files for padding */
  262.             break; /* skip 'em, a THEADR will show up soon */
  263.       default :
  264.             do_unimplt();
  265.             break;
  266.      }
  267.      c = get_obj_byte();
  268.    }
  269. return(0);
  270. }
  271.  
  272. /************************************************
  273. * get a byte from the object code file pointer  *
  274. ************************************************/
  275. byte get_obj_byte()
  276. {
  277. word w;
  278.  c = (byte)(w =  getc(fp1));
  279.  stoppit = (w == EOF);
  280.  chksum_count = chksum_count + c;
  281.  return(c);
  282. }
  283.  
  284. /************************************************
  285. * get a word from the object code file pointer  *
  286. ************************************************/
  287. word get_obj_word(swap)
  288. byte swap;
  289. {                                /* input -  Hi Lo */
  290.  if (swap)
  291.